package org.example.springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.example.springboot.entity.Collection;

import java.util.List;
import java.util.Map;

/**
 * 收藏Mapper接口
 */
@Mapper
public interface CollectionMapper extends BaseMapper<Collection> {
    
    /**
     * 获取热门收藏图书ID及其收藏次数
     * @param limit 限制数量
     * @return 图书ID和收藏次数列表
     */
    @Select("SELECT book_id as bookId, COUNT(*) as count FROM collection GROUP BY book_id ORDER BY count DESC LIMIT #{limit}")
    List<Map<String, Object>> getHotCollectionBooks(@Param("limit") Integer limit);
    
    /**
     * 获取指定图书ID列表的收藏次数
     * @param bookIds 图书ID列表
     * @return 图书ID和收藏次数列表
     */
    @Select("<script>SELECT book_id as bookId, COUNT(*) as count FROM collection WHERE book_id IN " +
            "<foreach item='item' collection='bookIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            " GROUP BY book_id ORDER BY count DESC</script>")
    List<Map<String, Object>> getHotCollectionBooksWithIds(@Param("bookIds") List<Long> bookIds);
} 